package itsm.isperp.module.controller.announcement; 

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.utils.HtmlUtil;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.paging.RenderPager;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.announcement.AnnoAnnouncement;
import itsm.isperp.module.entity.app.AppDict;
import itsm.isperp.module.service.announcement.AnnouncementService;
import itsm.isperp.module.service.app.AppDictService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2016-08-26 22:15
 */
@Controller
@RequestMapping("announcement/announcement")
public class AnnouncementController extends BaseController<AnnoAnnouncement> {

	@Autowired
	protected AnnouncementService announcementService;

	@Override
	public AnnouncementService getService() {
		return this.announcementService;
	}
	
	@Override
	public String getPageTitle() {
		return "公告";
	}
	
	
	@RequestMapping("infoView")
	public ModelAndView infoView(String id, HttpServletRequest req) {
		ModelAndView mv = new ModelAndView(
				"announcement/announcement/infoView");
		String knowledgeId = req.getParameter("id");

		Map<String, Object> model = announcementService
				.findOneDetailById(knowledgeId);
		mv.addObject("model", model);
		return mv;
	}
	
	
	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();

		AnnoAnnouncement model = null;
		if (StringUtils.isEmpty(id)) {
			model = new AnnoAnnouncement();
			model.setId(HibernateUUIDGenerator.generate());
		} else
		{
			model=this.getService().findOne(id);
		}
		AppDictService appDictService = ContextHolder
				.getSpringBean("appDictService");
		List<AppDict> importance = appDictService
				.findByType("ANNOUNCE_IMPORTANCE_LEVEL");
		List<AppDict> status = appDictService
				.findByType("ANNOUNCE_STATUS");
		mv.addObject("model",model);
		mv.addObject("importanceLevel",importance);
		mv.addObject("status",status);
		return mv;
	}
	
	@RequestMapping("getData")
	@ResponseBody
	public List<AnnoAnnouncement> getData(HttpServletRequest req)
			throws Exception {
		String rownum=req.getParameter("rownum");
		if(StringUtils.isEmpty(rownum))
			rownum="5";
		AnnouncementService announcementService = ContextHolder.getSpringBean("announcementService");
		List<AnnoAnnouncement> klg = announcementService.getNewTop10Item(Integer.parseInt(rownum));
		return klg;
	}
	
	
	
	@RequestMapping("searchResultView")
	public ModelAndView searchResult(HttpServletRequest req) {
		ModelAndView mv = new ModelAndView(
				"announcement/announcement/searchResultView");
		String keyword = req.getParameter("keyword");
		String pageIndex = req.getParameter("page");
		mv.addObject("keyword", keyword);

		int currentPage = 1;
		if (StringUtils.isNotEmpty(pageIndex)) {
			currentPage = Integer.parseInt(pageIndex);
		} 
		Pageable pg = null;
		if (StringUtils.isEmpty(keyword)) {
			pg = new PageRequest(currentPage, 5);
			

			Page<AnnoAnnouncement> r = this.getService().findAll().toPage();
			
			for (AnnoAnnouncement d : r) {
				d.setContents(HtmlUtil.delHTMLTag(d.getContents()));
			}

			mv.addObject(
					"pager",
					RenderPager.renderHtml(req, currentPage, 5,
							 r.getContent().size()));

			mv.addObject("result", r);
			return mv;
		}else
		{
			pg = new PageRequest(currentPage - 1, 5);
			JqGridRequest request = new JqGridRequest(pg);
			request.setSearchString(" (title like '%"+keyword+"%' or contents like '%"+keyword+"%') ");
			Page<AnnoAnnouncement> page = this.getService().findAll(request).toPage();
			
			for (AnnoAnnouncement d : page) {
				d.setContents(HtmlUtil.delHTMLTag(d.getContents()));
			}

			mv.addObject("pager", RenderPager.renderHtml(req, currentPage,
					pg.getPageSize(), page.getContent().size()));
			mv.addObject("result", page);
			return mv;
			
		}
		

	}
}
